//
//  ContactManager.m
//  HomeWork6-1
//
//  Created by TravelRound on 15/10/30.
//  Copyright © 2015年 TravelRound. All rights reserved.
//

#import "ContactManager.h"
#import "Contact.h"

@implementation ContactManager

- (id)init
{
    self = [super init];
    if (self) {
        self.dataDic = [NSMutableDictionary dictionary];

//        
//        NSArray *arr = [NSArray arrayWithObjects:@"A", @"B", nil];
//        for (NSInteger i = 0; i < arr.count; i++) {
//            
//            NSMutableArray *tempArr = [NSMutableArray array];
//            [self.dataDic setValue:tempArr forKey:[arr objectAtIndex:i]];
//            
//        }
        
        
    }
    return self;
}

- (void)addContact:(Contact *)contact
{
    if (0 == contact.name.length) {
        return;
    }
    NSString *key = [contact.name substringToIndex:1];
    NSMutableArray *arr = nil;
    
    if (nil == [self.dataDic objectForKey:key]) {
        
        // 创建联系人数组
        arr = [NSMutableArray array];
        // 数组添加到字典中
        [self.dataDic setValue:arr forKey:key];
        
    } else {
        arr = [self.dataDic objectForKey:key];
    }
    // 联系人添加到数组中
    [arr addObject:contact];
    
}


- (NSMutableArray *)getSortedNameOfKey:(NSString *)key
{
    // 判断参数的有效性
    if (0 == key.length) {
        return nil;
    }
    NSMutableArray *arr = [self.dataDic objectForKey:key];
    
    [arr sortUsingSelector:@selector(compareByNameOfContact:)];
    
    return arr;
}

- (NSMutableArray *)searchContactWithPhone:(NSString *)phone
{
    if (0 == phone.length) {
        return nil;
    }
    
    NSMutableArray *arr = [NSMutableArray array];
    
    // 遍历字典得到联系人数组
    for (NSString *key in self.dataDic) {
        // 遍历联系人数组得到联系人
        for (Contact *contact in [self.dataDic objectForKey:key]) {
            if ([contact.phone isEqualToString:phone]) {
                // 若电话匹配, 则添加进数组中
                // 防止不同联系人电话相同
                [arr addObject:contact];
            }
        }
    }
    return arr;
}

- (NSMutableArray *)getAllGirlsSortedOfAge
{
    NSMutableArray *arr = [NSMutableArray array];
    
    // 遍历字典得到联系人数组
    for (NSString *key in self.dataDic) {
        // 遍历联系人数组得到联系人
        for (Contact *contact in [self.dataDic objectForKey:key]) {
            if ([contact.sex isEqualToString:@"girl"]) {
                // 若为女性, 则添加进数组中
                [arr addObject:contact];
            }
        }
    }
    [arr sortUsingSelector:@selector(compareByAgeOfContact:)];
    return arr;
    
    
}

- (void)deleteContactWithName:(NSString *)name
{
    if (0 == name.length) {
        return;
    }
    // 根据名字获取key(分组名)
    NSString *key = [name substringToIndex:1];
    // 获取分组名对应的数组
    NSMutableArray *arr = [self.dataDic objectForKey:key];
    for (NSInteger i = 0; i < arr.count; i++) {
        if ([[[arr objectAtIndex:i] name] isEqualToString:name]) {
            // 若名字符合则删除
            [arr removeObjectAtIndex:i];
            // 注意, 防止漏删
            i--;
        }
    }
}

- (void)deleteGroupWithKey:(NSString *)key
{
    if (0 == key) {
        return;
    }
    [self.dataDic removeObjectForKey:key];
}

- (void)allContactInfo
{
    for (NSString *key in self.dataDic) {
        for (Contact *contact in [self.dataDic objectForKey:key]) {
            NSLog(@"%@ %@ %@ %ld", contact.name, contact.sex, contact.phone, contact.age);
        }
    }
    
    
}

@end
